#include "cclientchoosecar.h"
#include <QDataStream>
#include <QTcpSocket>
#include "protocol/ClientChooseCar.h"

CClientChooseCar::CClientChooseCar(QObject *parent)
    :INetWorking(parent)
{
}

CClientChooseCar::~CClientChooseCar()
{
}

void CClientChooseCar::prepareWriteData(QByteArray* block, void* param)
{
    // we cannot discrete this definition to the outside of the block,
    // and pass the reference to the funtion, or it will cause some
    // unknown problems, such as wrong streams. Maybe it is a bug of QT.
    QDataStream out(block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_3);


    protocal::ClientChooseCar* pClientChooseCar = (protocal::ClientChooseCar*)param;

    // output the head information
    out << (qint32)(pClientChooseCar->source) \
        << (qint32)(pClientChooseCar->cli_command) \
        << (qint32)(pClientChooseCar->size);

    // output the body information

    //
    char * temp = new char [10];
    // we must do so, because the constructor of QString(char*) will truncate the
    // remaining space after '\0', and we need them to occupy the space.
    memcpy(temp, pClientChooseCar->userID, 10);
    out << QString(temp);

    memcpy(temp, pClientChooseCar->carID, 10);
    out << QString(temp);

    delete [] temp;
}

bool CClientChooseCar::prepareReadData(QTcpSocket *pTcpSocket, protocal::BasePtc *basePtc)
{
    QDataStream in(pTcpSocket);

    if (   basePtc->source != protocal::CLIENT\
        || basePtc->cli_command != protocal::CLI_LOGIN\
        || basePtc->size != sizeof(protocal::ClientChooseCar))
    {
        return false;
    }

    source = basePtc->source;
    command = basePtc->cli_command;

    formResult();

    return true;
}

void CClientChooseCar::formResult()
{

}


